﻿using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Messaging;
using SnmpForm.DbWrapper.DbDataTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;

namespace SnmpForm.ApIpPool
{
    class ApIpPoolTask:ApSearch.RequestTask
    {
        private MainForm _objMainForm = null;
        public ApIpPoolTask(MainForm objMainForm, List<SearchTarget> list)
            : base(list)
        {
            _objMainForm = objMainForm;
        }
        public override void RecvEvent(EndPoint remote, ISnmpMessage response)
        {
            foreach (Variable v in response.Pdu().Variables)
            {
                Console.WriteLine(remote.ToString());
                Console.WriteLine(v);
                if (v.Data.TypeCode != SnmpType.Null)
                {
                    DbWrapper.DbDataTypes.Ap apinfo = new Ap();
                    apinfo.manage_ip = ((IPEndPoint)remote).Address.ToString();
                    apinfo.mark = v.Data.ToString();
                    _objMainForm.Invoke(_objMainForm.updateApIpPool, new Object[] { apinfo });
                }
            }
        }

        public override void TaskMain()
        {
            int nInterval =
                    (int)Properties.Settings.Default.ApIpPoolInterval;
            while (true)
            {
                ResetTask();
                base.TaskMain();
                DateTime timePre = DateTime.Now;
                double nSpan = 0;
                do{
                    Recv(nInterval*1000);
                    nSpan = DateTime.Now.Subtract(timePre).TotalSeconds;
                }while(nSpan<nInterval);
            }
        }
    }
}
